package com.wentao.thisService.service.impl;

import com.wentao.thisFacade.common.enums.ExceptionCode;
import com.wentao.thisFacade.common.exception.CustomException;
import com.wentao.thisFacade.domain.result.GoodsInfoResult;
import com.wentao.thisService.common.TokenVerify;
import com.wentao.thisService.mapper.GoodsDao;
import com.wentao.thisService.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: ZhaiYanTao
 * @Date: 2020/3/27 10:17
 * @Description: 商品service实现
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Value("${readFile.host.ip}")
    private String readFileHostIp;

    @Autowired
    private GoodsDao goodsDao;

    /**
     * @see GoodsService#goodsList()
     */
    @Override
    @TokenVerify
    public List<GoodsInfoResult> goodsList() {
        List<Map<String, Object>> goodsInfoMapList = goodsDao.SearchGoodsInfo();

        return transformMapToGoodsInfoList(goodsInfoMapList);
    }

    /**
     * @see GoodsService#goodsPropertyInfo(Long)
     */
    @Override
    @TokenVerify
    public GoodsInfoResult goodsPropertyInfo(Long goodsId) {

        List<Map<String, Object>> goodsInfoMapList = goodsDao.SearchGoodsInfoByGoodsId(goodsId);

        if (null == goodsInfoMapList || goodsInfoMapList.size() == 0) {
            throw new CustomException(ExceptionCode.NO_GOODS_INFO_ERROR);
        }

        return transformMapToGoodsInfoList(goodsInfoMapList).get(0);
    }

    /**
     * 查询结果map转换为商品信息列表
     *
     * @param goodsInfoMapList 数据库查询结果map
     * @return 商品信息列表
     */
    private List<GoodsInfoResult> transformMapToGoodsInfoList(List<Map<String, Object>> goodsInfoMapList) {
        Map<String, GoodsInfoResult> goodsInfoResultMap = new HashMap<>();
        if (null != goodsInfoMapList && goodsInfoMapList.size() > 0) {
            for (Map<String, Object> stringObjectMap : goodsInfoMapList) {
                Long id = (Long) stringObjectMap.get("id");
                String title = (String) stringObjectMap.get("title");
                String unit = (String) stringObjectMap.get("unit");
                String attributeType = (String) stringObjectMap.get("attributeType");
                String attributeValue = (String) stringObjectMap.get("attributeValue");
                Long price = (Long) stringObjectMap.get("price");
                Long discount = (Long) stringObjectMap.get("discount");

                GoodsInfoResult goodsInfoResult = goodsInfoResultMap.get(id.toString());
                if (null == goodsInfoResult) {
                    goodsInfoResult = new GoodsInfoResult();
                    goodsInfoResult.setId(id.toString());
                    goodsInfoResult.setRotationPictureList(new ArrayList<>());
                    goodsInfoResult.setDetailsPictureList(new ArrayList<>());
                }
                goodsInfoResult.setTitle(title);
                goodsInfoResult.setUnit(unit);
                goodsInfoResult.setOriginalPrice(price);
                goodsInfoResult.setDiscountPrice(discount);
                if ("producingArea".equals(attributeType)) {
                    goodsInfoResult.setProducingArea(attributeValue);
                }
                if ("alcoholContent".equals(attributeType)) {
                    goodsInfoResult.setAlcoholContent(attributeValue);
                }
                if ("storageCondition".equals(attributeType)) {
                    goodsInfoResult.setStorageCondition(attributeValue);
                }
                if ("imported".equals(attributeType)) {
                    goodsInfoResult.setImported(attributeValue);
                }
                if ("notes".equals(attributeType)) {
                    goodsInfoResult.setNotes(attributeValue);
                }
                if ("serialNumber".equals(attributeType)) {
                    goodsInfoResult.setSerialNumber(attributeValue);
                }
                if ("producingArea".equals(attributeType)) {
                    goodsInfoResult.setProducingArea(attributeValue);
                }
                if ("rotationPicture".equals(attributeType)) {
                    // 修改返回结果，让结果能直接在网页打开
                    String matchChar = "\\";
                    String replaceChar = "/";
                    if (attributeValue.indexOf(matchChar) > 0) {
                        attributeValue = String.format("%s%s", readFileHostIp, attributeValue.replace(matchChar, replaceChar));
                    }
                    goodsInfoResult.getRotationPictureList().add(attributeValue);
                }
                if ("detailsPicture".equals(attributeType)) {
                    // 修改返回结果，让结果能直接在网页打开
                    String matchChar = "\\";
                    String replaceChar = "/";
                    if (attributeValue.indexOf(matchChar) > 0) {
                        attributeValue = String.format("%s%s", readFileHostIp, attributeValue.replace(matchChar, replaceChar));
                    }
                    goodsInfoResult.getDetailsPictureList().add(attributeValue);
                }

                goodsInfoResultMap.put(goodsInfoResult.getId(), goodsInfoResult);
            }
        }

        return new ArrayList<>(goodsInfoResultMap.values());
    }
}
